package daily;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BlockingQueueTest {

	public static void main(String[] args) {
		final BlockingQueue<Integer> blockingQueue=new ArrayBlockingQueue<Integer>(3);
		
		ExecutorService threadPool=Executors.newFixedThreadPool(2);
		for(int i=0;i<2;i++){
			threadPool.submit(new Runnable(){

				@Override
				public void run() {
					while(true){
						try {
							Thread.sleep(100);
							System.out.println(Thread.currentThread().getName()+" 准备放数据，目前队列数据数为 "+blockingQueue.size());
							blockingQueue.put(1);
							System.out.println(Thread.currentThread().getName()+" 放了一个数据，目前队列数据数为 "+blockingQueue.size());
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					
				}
				
			});
			
			threadPool.submit(new Runnable(){

				@Override
				public void run() {
					while(true){
						try {
							Thread.sleep(1000);
							System.out.println(Thread.currentThread().getName()+" 准备取数据，目前队列数据数为 "+blockingQueue.size());
							blockingQueue.take();
							System.out.println(Thread.currentThread().getName()+" 取了一个数据，目前队列数据数为 "+blockingQueue.size());
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					
				}
				
			});
		}
	}

}
